﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using br.com.SistemaChamados;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Model.Common;
using br.com.SistemaChamados.Enum;

public partial class Pages_CallMain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //ClearFields();
        if (!IsPostBack)
        {
            Session["LoggedUser"] = 1;
            FillDdlCategory();
        }        
    }

    private void FillDdlSubCategory()
    {
        CallSubCategoryController callSubCategoryController = new CallSubCategoryController();
        CallCategory callCategory = new CallCategory();
        CallSubCategory callSubCategory = new CallSubCategory();
        if (Convert.ToInt32(ddlCallCategory.SelectedIndex) != 0)
        {
            callCategory.Id = Convert.ToInt32(ddlCallCategory.SelectedValue);
            callSubCategory.CallCategory = callCategory;
            ddlCallSubCategory.DataSource = callSubCategoryController.GetCallSubCategoriesByCallCategory(callSubCategory);
            ddlCallSubCategory.DataTextField = "Description";
            ddlCallSubCategory.DataValueField = "Id";
            ddlCallSubCategory.DataBind();
            ddlCallSubCategory.Items.Insert(0, "Selecione");
        }
        else
        {
            ClearDdlCallSubCategory();
        }
    }

    private void FillDdlCategory()
    {
        CallCategoryController callCategoryController = new CallCategoryController();
        ddlCallCategory.DataSource = callCategoryController.GetCallCategories();
        ddlCallCategory.DataTextField = "Description";
        ddlCallCategory.DataValueField = "Id";
        ddlCallCategory.DataBind();
        ddlCallCategory.Items.Insert(0, "Selecione");
    }

    private void ClearDdlCallSubCategory()
    {
        for (int i = 0; i < ddlCallSubCategory.Items.Count; i++)
        {
            ddlCallSubCategory.Items[i].Selected = false;
        }
        ddlCallSubCategory.Items[0].Selected = true;
        ddlCallSubCategory.DataSource = "";
        ddlCallSubCategory.DataBind();
    }

    private void ClearFields()
    {
        txbId.Text = "";
        txbDataOpening.Text = "";
        txbProblemDescription.Text = "";
    }
    
    protected void ddlCallCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillDdlSubCategory();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Seach();
    }

    private void Seach()
    {
        List<Call> listCall = new List<Call>();
        CallController callController = new CallController();
        int? callId = null;
        DateTime? dateOpening = null;
        string problemDescription = "";
        int? categoryId = null;
        int? subCategoryId = null;
        try
        {
            string userId = Session["LoggedUser"].ToString();
            User userOpen = new User();
            int userOpenId = Convert.ToInt32(userId);
            if (txbId.Text != "")
            {
                callId = Convert.ToInt32(txbId.Text);
            }
            else if (txbDataOpening.Text != "")
            {
                dateOpening = Convert.ToDateTime(txbDataOpening.Text);
            }
            else if (txbProblemDescription.Text != "")
            {
                problemDescription = Convert.ToString(txbProblemDescription.Text);
            }
            else if (ddlCallCategory.SelectedIndex != 0)
            {
                categoryId = Convert.ToInt32(ddlCallCategory.SelectedValue);
            }
            else if (ddlCallSubCategory.SelectedIndex != 0)
            {
                subCategoryId = Convert.ToInt32(ddlCallSubCategory.SelectedValue);
            }
            
            listCall = callController.GetCallByUser(userOpenId, callId.Value, dateOpening.Value, problemDescription, categoryId.Value, subCategoryId.Value);
            CallCategoryController callCategoryController = new CallCategoryController();
            ddlCallCategory.DataSource = callCategoryController.GetCallCategories();
            ddlCallCategory.DataTextField = "Description";
            ddlCallCategory.DataValueField = "Id";
            ddlCallCategory.DataBind();
            ddlCallCategory.Items.Insert(0, "Selecione");
        }
        catch (Exception ex)
        {
            
            throw ex;
        }        
    }  
}